package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TBDevice;
import com.ruoyi.system.domain.TBOrderDevice;
import com.ruoyi.system.domain.TBOrderSpread;
import com.ruoyi.system.mapper.TBOrderDeviceMapper;
import com.ruoyi.system.mapper.TBOrderSpreadMapper;
import com.ruoyi.system.service.ITBDeviceService;
import com.ruoyi.system.service.ITBOrderDeviceService;
import com.ruoyi.system.service.ITBOrderSpreadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBOrderMapper;
import com.ruoyi.system.domain.TBOrder;
import com.ruoyi.system.service.ITBOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author pspyy
 * @date 2022-03-09
 */
@Service
public class TBOrderServiceImpl implements ITBOrderService 
{
    @Autowired
    private TBOrderMapper tBOrderMapper;
    @Autowired
    private ITBOrderSpreadService tBOrderSpreadService;
    @Autowired
    private ITBOrderDeviceService tBOrderDeviceService;
    @Autowired
    private ITBDeviceService tbDeviceService;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public TBOrder selectTBOrderById(Long id)
    {
        TBOrder tbOrder = tBOrderMapper.selectTBOrderById(id);
        List<Long> deviceIds = tBOrderDeviceService.selectDeviceIdList(tbOrder.getOrderNum());
        TBOrderSpread tbOrderSpread= new TBOrderSpread();
        tbOrderSpread.setOrderNum(tbOrder.getOrderNum());
        List<TBOrderSpread> orderSpreadList = tBOrderSpreadService.selectTBOrderSpreadList(tbOrderSpread);
        tbOrder.setOrderSpreadList(orderSpreadList);
        tbOrder.setDeviceIds(deviceIds);
        return tbOrder;
    }

    /**
     * 查询订单列表
     * 
     * @param tBOrder 订单
     * @return 订单
     */
    @Override
    public List<TBOrder> selectTBOrderList(TBOrder tBOrder)
    {
        List<TBOrder> tbOrders = tBOrderMapper.selectTBOrderList(tBOrder);
        return tbOrders;
    }

    /**
     * 新增订单
     * 
     * @param tBOrder 订单
     * @return 结果
     */
    @Override
    public int insertTBOrder(TBOrder tBOrder)
    {
        tBOrder.setCreateTime(DateUtils.getNowDate());
        int i = tBOrderMapper.insertTBOrder(tBOrder);
        if(i > 0){
            //循环插入订单分期表
            for (TBOrderSpread os:tBOrder.getOrderSpreadList()) {
                os.setOrderNum(tBOrder.getOrderNum());
               os.setCreateTime(DateUtils.getNowDate());
                tBOrderSpreadService.insertTBOrderSpread(os);
            }
            //循环插入订单设备表
            for (Long id:tBOrder.getDeviceIds()) {
                //修改设备出售状态
                TBDevice tbDevice = tbDeviceService.selectTBDeviceById(id);
                tbDevice.setIsSell("1");//已出售
                TBOrderDevice orderDevice = new TBOrderDevice();
                orderDevice.setOrderNum(tBOrder.getOrderNum());
                orderDevice.setDeviceId(id);
                orderDevice.setDeviceStatue("0");//就绪状态
                orderDevice.setCreateTime(DateUtils.getNowDate());
                tBOrderDeviceService.insertTBOrderDevice(orderDevice);
            }
        }
        return i;
    }

    /**
     * 修改订单
     * 
     * @param tBOrder 订单
     * @return 结果
     */
    @Override
    public int updateTBOrder(TBOrder newOrder)
    {
        //先查询修改前得数据
        TBOrder oldOrder = tBOrderMapper.selectTBOrderById(newOrder.getId());
        newOrder.setUpdateTime(DateUtils.getNowDate());
        int i = tBOrderMapper.updateTBOrder(newOrder);
        if(i > 0){

            //判断两次操作前后付款方式一不一样
            if(oldOrder.getPayMethod().equals(newOrder.getPayMethod())){
                //查询订单分期表中有无数据
                List<Long> orderSpreadIdList = tBOrderSpreadService.selectOrderSpreadIdList(newOrder.getOrderNum());
                if(orderSpreadIdList.size() > 0){
                    //先删除订单分期表数据
                    int orderSpreadNum = tBOrderSpreadService.deleteTBOrderSpreadByOrderNum(newOrder.getOrderNum());
                    if(orderSpreadNum > 0){
                        //循环插入订单分期表
                        for (TBOrderSpread os:newOrder.getOrderSpreadList()) {
                            os.setOrderNum(newOrder.getOrderNum());
                            os.setCreateTime(DateUtils.getNowDate());
                            tBOrderSpreadService.insertTBOrderSpread(os);
                        }
                    }
                }else{
                    //循环插入订单分期表
                    for (TBOrderSpread os:newOrder.getOrderSpreadList()) {
                        os.setOrderNum(newOrder.getOrderNum());
                        os.setCreateTime(DateUtils.getNowDate());
                        tBOrderSpreadService.insertTBOrderSpread(os);
                    }
                }
            }else{
                //数据修改订单支付方式由全款修改成分期付款
                if("0".equals(oldOrder.getPayMethod()) && "1".equals(newOrder.getPayMethod())){
                    //循环插入订单分期表
                    for (TBOrderSpread os:newOrder.getOrderSpreadList()) {
                        os.setOrderNum(newOrder.getOrderNum());
                        os.setCreateTime(DateUtils.getNowDate());
                        tBOrderSpreadService.insertTBOrderSpread(os);
                    }
                }else if("1".equals(oldOrder.getPayMethod()) && "0".equals(newOrder.getPayMethod())){
                    //数据修改订单支付方式由分期付款修改为全款
                    //直接删除订单分期表数据
                    tBOrderSpreadService.deleteTBOrderSpreadByOrderNum(newOrder.getOrderNum());
                }
            }

            //根据订单号查询已存在的关联关系ids
            List<Long> oldDevIds = tBOrderDeviceService.selectDeviceIdList(newOrder.getOrderNum());

            //判断当前设备状态；0：未入库；1：设备售出

            if("0".equals(newOrder.getDeviceState())){//未入库
                if(oldDevIds!=null&&oldDevIds.size()>0){
                    for (Long devId:oldDevIds) {
                        TBDevice tbDevice = tbDeviceService.selectTBDeviceById(devId);
                        tbDevice.setIsSell("0");//未出售
                    }
                }
                //删除已关联的设备关系
                tBOrderDeviceService.deleteTBOrderDeviceByOrderNum(newOrder.getOrderNum());
            }else{//设备售出
                List<Long> currDevIds = newOrder.getDeviceIds();//当前选择的设备ids
                //比较出有哪些是新增的设备(currDevIds，之前关联的oldDevIds不存在)
                //哪些是删除的设备(之前关联的oldDevIds存在，currDevIds不存在)
                //将新增的录入，删除的删除，同时修改设备的出售状态

                //根据 devIds 与 currDevIds过滤
                List<Long> addIds=new ArrayList<>();
                List<Long> removeIds=new ArrayList<>();

                //现在有过去没有的设备关系，放到addIds中
                for (Long curId:currDevIds) {
                    boolean has=false;
                    for (Long oldId:oldDevIds) {
                        if(curId==oldId){//判断是否有已存在的id
                            has=true;
                            break;
                        }
                    }
                    if(!has){
                        addIds.add(curId);
                    }
                }

                //过去有现在没有的设备关系，放到removeIds中
                for (Long oldId:oldDevIds) {
                    boolean has=false;
                    for (Long curId:currDevIds) {
                        if(curId==oldId){//判断是否有已存在的id
                            has=true;
                            break;
                        }
                    }
                    if(!has){
                        removeIds.add(oldId);
                    }
                }
                //处理新增关系
                for (Long id:addIds) {
                    //新增设备关系
                    TBOrderDevice orderDevice = new TBOrderDevice();
                    orderDevice.setOrderNum(newOrder.getOrderNum());
                    orderDevice.setDeviceId(id);
                    orderDevice.setDeviceStatue("0");//就绪
                    orderDevice.setCreateTime(DateUtils.getNowDate());
                    tBOrderDeviceService.insertTBOrderDevice(orderDevice);
                    //修改设备出售状态
                    TBDevice tbDevice = tbDeviceService.selectTBDeviceById(id);
                    tbDevice.setIsSell("1");//已出售
                    tbDeviceService.updateTBDevice(tbDevice);
                }
                //处理删除的关系
                for (Long id:removeIds) {
                    //根据设备id和订单号删除
                    String orderNum = newOrder.getOrderNum();
                    tBOrderDeviceService.deleteByNoAndDevId(orderNum,id);//删除设备关联关系
                    //修改设备出售状态
                    TBDevice tbDevice = tbDeviceService.selectTBDeviceById(id);
                    tbDevice.setIsSell("0");//未出售
                    tbDeviceService.updateTBDevice(tbDevice);
                }


            }

        }
        return i;
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTBOrderByIds(Long[] ids)
    {
        //先查询出要删除订单订单编号
        List<String> orderNums = tBOrderMapper.selectOrderNumById(ids);
        int i = tBOrderMapper.deleteTBOrderByIds(ids);
        //订单删除成功后删除订单关联的设备数据
        if(i  > 0){
            //删除已关联的设备关系
            for (int j = 0; j < orderNums.size(); j++) {
                tBOrderDeviceService.deleteTBOrderDeviceByOrderNum(orderNums.get(j));
            }

        }
        return i;
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteTBOrderById(Long id)
    {
        return tBOrderMapper.deleteTBOrderById(id);
    }

    @Override
    public int selectOrderByCustomerIds(Long[] ids) {
        return tBOrderMapper.selectOrderByCustomerId(ids);
    }

    @Override
    public int checkOrderNumUnique(String orderNum) {
        return tBOrderMapper.checkOrderNumUnique(orderNum);
    }
}
